package com.example.roomdatabasetest.recyclerView;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.roomdatabasetest.Car;
import com.example.roomdatabasetest.CarMode;
import com.example.roomdatabasetest.R;

import java.util.ArrayList;
import java.util.List;

public class CarRecyclerViewAdapter extends RecyclerView.Adapter {

    List<Car> cars;

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

    public CarRecyclerViewAdapter(List<Car> cars) {
        this.cars = cars;
    }
    // 构建holder
    static class CarViewHolder extends RecyclerView.ViewHolder{
        private TextView tvCarId;
        private TextView tvCarBrand;
        private TextView tvCarPrice;
        private Button btnDelete;
        private Button btnUpdate;

        public CarViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCarId = itemView.findViewById(R.id.tv_car_id);
            tvCarBrand = itemView.findViewById(R.id.tv_car_brand);
            tvCarPrice = itemView.findViewById(R.id.tv_car_price);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            btnUpdate = itemView.findViewById(R.id.btn_update);
        }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View  view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_info,parent,false);
       CarViewHolder carViewHolder = new CarViewHolder(view);
       Car car = cars.get(viewType);
       carViewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               AlertDialog.Builder builder = new AlertDialog.Builder(parent.getContext());
               builder.setTitle("Are u sure Delete that car??").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       cars.remove(viewType);
                       CarMode.getInstance(parent.getContext()).deleteItem(car);
                       notifyItemRemoved(viewType);
                       dialog.dismiss();//消失
                   }
               }).setNegativeButton("Wait", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       dialog.dismiss();
                   }
               }).create().show();

           }
       });
       carViewHolder.btnUpdate.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
           // 进行更新
               Car car = cars.get(viewType);
               String carBrand = car.getBrand();
               String carPrice = String.valueOf(car.getPrice());

               final View view = LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_diy, null);
               EditText editUpdateBrand;
               EditText editUpdatePrice;
               Button btnUpdate;
               Button btnQuit;

               editUpdateBrand = view.findViewById(R.id.edit_update_brand);
               editUpdatePrice = view.findViewById(R.id.edit_update_price);//注意这个View和参数里带的view是不一样的
               btnUpdate = view.findViewById(R.id.btn_update);
               btnQuit =view.findViewById(R.id.btn_quit);


               final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
               AlertDialog dialog = builder.setTitle("").setView(view).show();
               editUpdateBrand.setText(carBrand);
               editUpdatePrice.setText(carPrice);
               btnQuit.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       dialog.dismiss();
                   }
               });
               btnUpdate.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Car car = cars.get(viewType);
                       String brand = editUpdateBrand.getText().toString();
                       String price = editUpdatePrice.getText().toString();
                       car.setBrand(brand);
                       car.setPrice(Integer.parseInt(price));
                       CarMode.getInstance(parent.getContext()).updateItem(car);
                       dialog.dismiss();
                       notifyItemChanged(viewType);
                   }
               });
           }
       });
       return carViewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        return  position;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Car car = cars.get(position);
        CarViewHolder carViewHolder = (CarViewHolder) holder;
        carViewHolder.tvCarId.setText(String.valueOf(car.getId()));
        carViewHolder.tvCarBrand.setText(car.getBrand());
        carViewHolder.tvCarPrice.setText(String.valueOf(car.getPrice()));
    }

    @Override
    public int getItemCount() {
        return cars.size();
    }

}
